package 高效制胜._05;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/9/11 21:11
 * 132 模式
 * 算法：单调栈
 */
public class _456 {

    public boolean find132pattern_violence(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                for (int k = j + 1; k < n; k++) {
                    if (nums[i] < nums[k] && nums[k] < nums[j]) return true;
                }
            }
        }
        return false;
    }

    public boolean find132pattern(int[] nums) {
        Deque<Integer> stack = new ArrayDeque<>();
        int k = Integer.MIN_VALUE;
        for (int i = nums.length - 1; i >= 0; i--) {
            if (k > nums[i]) return true;
            while (!stack.isEmpty() && nums[i] > stack.peek()) k = Math.max(k, stack.pop());
            stack.push(nums[i]);
        }
        return false;
    }

}
